<?php
namespace Mhome\Controller;
use Common\Controller\BaseController;
use Common\Model\CommonModel;

class LoginController extends BaseController {

    /**
     * 登陆方法
     */
    public function loginAction(){
        $username = $_REQUEST['username']?$_REQUEST['username']:$this->errorReturn("请输入账号");
        $password = $_REQUEST['password']?md5($_REQUEST['password']):$this->errorReturn("请输入密码");

        $field = "user_id,depart_id,name,password,phone,email,rank,station,user_type,use_start_time,use_end_time";
        $user_data = M("User")
            ->where(['username'=>$username,"password"=>$password])
            ->field($field)
            ->find();

        if(!$user_data){
            $user_data = M("User")
                ->where(['phone'=>$username,"password"=>$password])
                ->field($field)
                ->find();
            if(!$user_data){
                $user_data = M("User")
                    ->where(['email'=>$username,"password"=>$password])
                    ->field($field)
                    ->find();
                if(!$user_data){
                    $this->errorReturn("账号或密码错误");
                }
            }
        }

        //如果是临时账号则判断有效时间
        $current_time = time();
        if($user_data['user_type'] == 0){
            if($user_data['use_start_time'] > $current_time){
                $this->errorReturn("该账号还未生效");
            }
            if($user_data['use_end_time'] < $current_time){
                $this->errorReturn("该账号已失效");
            }
        }

        //处理用户数据返回给前端
        $user_data['depart_name'] = M("Department")
            ->where(['depart_id'=>$user_data['depart_id']])
            ->getField("name");
        if(!$user_data['depart_name']){
            $user_data['depart_name'] = "";
        }
        unset($user_data['depart_id']);

        $user_data['rank'] = CommonModel::rank_str_array[$user_data['rank']];

        if($user_data['user_type'] == 1){
            //长期账号
            $user_data['use_time'] = "长期有效";
            $user_data['user_type'] = "一般账号";
        }else{
            //临时账号
            $user_data['use_time'] = changeDateTime($user_data['use_start_time'])."--".changeDateTime($user_data['use_end_time']);
            $user_data['user_type'] = "临时账号";
        }
        unset($user_data['use_start_time']);
        unset($user_data['use_end_time']);

        $token = getToken();

        $update_token = M("User")
            ->where(['user_id'=>$user_data['user_id']])
            ->setField("token",$token);
        if(!$update_token){
            $this->errorReturn("获取登陆标识失败，请重新尝试");
        }
        $user_data['token'] = $token;
        unset($user_data['user_id']);

        if($user_data['phone']){
            $start_password = md5(substr($user_data['phone'],5,6));
        }else{
            $start_password = md5("123456");
        }
        if($start_password == $user_data['password']){
            $user_data['is_init_password'] = 1;
        }else{
            $user_data['is_init_password'] = 0;
        }
        unset($user_data['password']);

        $this->successReturn("登陆成功",$user_data);
    }
}
?>